package com.akilesa.dictcreator;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.ParagraphTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.TextExtractingVisitor;

import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: akilesa
 * Date: 26.01.2010
 * Time: 15:37:37
 * To change this template use File | Settings | File Templates.
 */
public class LingvoOnlineDictionarySupport implements OnlineDictionarySupport {

    private static final String GET_URL = "http://lingvo.abbyyonline.com/ru/en-ru";
    private Parser parser = new Parser();

    public String requestHtmlWithWordTranslation(String word) {
        String result = null;
        HttpClient httpClient = new HttpClient();
        HttpMethod method = new GetMethod(GET_URL + "/" + word);
        try {
            httpClient.executeMethod(method);
            result = method.getResponseBodyAsString();
            method.releaseConnection();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public String parseAndGetWordTranslation(String html) {
        String result = "";
        try {
            parser = new Parser(html);
            NodeList list = parser.extractAllNodesThatMatch(new NodeFilter() {

                public boolean accept(Node node) {
                    if (node instanceof Div) {
                        String className = ((Div) node).getAttribute("class");
                        if ("lol-card-content".equalsIgnoreCase(className)) {
                            return true;
                        }
                    }
                    return false;
                }
            });
            for (int i = 0; i < list.size(); i++) {
                result += getDictCard(list.elementAt(i));
            }
        } catch (ParserException e) {
            throw new RuntimeException(e);
        }        
        return result;
    }

    private String getDictCard(Node elem) throws ParserException {
        String result = "<div style=\"text-align:left\">";
        TagNode node = (Div) elem;
        NodeList list = node.getChildren();
        for (int i = 0; i < list.size(); i++) {
            if (list.elementAt(i) instanceof Div
                    && ((Div) list.elementAt(i)).getAttribute("class").equalsIgnoreCase("card-short")) {
                list = list.elementAt(i).getChildren();
                int pNum = 0;
                for (int j = 0; j < list.size(); j++) {
                    if (list.elementAt(j) instanceof ParagraphTag
                            && ((ParagraphTag) list.elementAt(j)).getAttribute("class").equalsIgnoreCase("P")) {
                        pNum++;
                        if (pNum > 1) {
                            result = result + extractText(list.elementAt(j));
                        }
                        continue;
                    }
                    if (list.elementAt(j) instanceof ParagraphTag
                            && ((ParagraphTag) list.elementAt(j)).getAttribute("class").equalsIgnoreCase("P1")) {
                        result = result + extractText(list.elementAt(j));
                    }
                }
                break;
            }
        }
        result+="</div>";
        return result;
    }

    private String extractText(Node elem) throws ParserException {
        String result = "";
        TagNode node = (TagNode) elem;
        parser = new Parser(node.toHtml());
        TextExtractingVisitor visitor = new TextExtractingVisitor();
        parser.visitAllNodesWith(visitor);
        result = result + visitor.getExtractedText() + "<BR>";
        return result;
    }
}
